package com.woniu.gf.pawn.shopweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.woniu.gf.pawn.comm.core.domain.Shop;
import com.woniu.gf.pawn.comm.core.domain.ShopStore;
import com.woniu.gf.pawn.comm.core.domain.Store;
import com.woniu.gf.pawn.pageparam.PageParam;
import com.woniu.gf.pawn.shopdto.ShopDto;
import com.woniu.gf.pawn.shopparam.ShopParam;
import com.woniu.gf.pawn.shopweb.dao.ShopMapper;
import com.woniu.gf.pawn.shopweb.dao.ShopStoreMapper;
import com.woniu.gf.pawn.shopweb.service.IShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TL
 * @since 2020-10-13
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopStoreMapper shopStoreMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplateObject;

    @Override
    public int addShop(ShopParam shopParam) {

        //新增一个门店
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopParam,shop);
        shopMapper.insert(shop);

        for (Store store : shopParam.getStores()) {
            ShopStore shopStore = new ShopStore();
            shopStore.setShopId(shop.getShopId());
            shopStore.setStoreId(store.getStoreId());
            shopStoreMapper.insert(shopStore);
        }
        //存入redis
        shopParam.setShopId(shop.getShopId());
        String shopJson = new Gson().toJson(shopParam);
        System.out.println(shopJson);
        stringRedisTemplate.opsForHash().put("shops","shop:"+shopParam.getShopId(),shopJson);
        stringRedisTemplate.opsForSet().add("shopIds",shopParam.getShopId().toString());
        return 0;
    }

    @Override
    public IPage<Shop> findShopInfo(PageParam pageParam) {
        //（当前页-1） × 条数
        Page<Shop> productEntityPage = new Page<>(pageParam.getPageIdx(), pageParam.getPageSize());

        return shopMapper.findShopInfo(productEntityPage,pageParam.getShopName(),pageParam.getManagerId(),pageParam.getStatus());
    }

    @Override
    public List<Store> findStore(Integer shopId) {
        return shopMapper.findStore(shopId);
    }

    @Override
    public ShopParam findOneShopInfo(Integer shopId) {
        Set<String> shopIds = stringRedisTemplate.opsForSet().members("shopIds");
        for (String id : shopIds) {
            if (Integer.parseInt(id) == shopId) {
                String shopJson = (String)stringRedisTemplate.opsForHash().get("shops", "shop:" + shopId);
                return new Gson().fromJson(shopJson, ShopParam.class);
            }
        }
        /*String shopJson = (String)stringRedisTemplate.opsForHash().get("shops", "shop:" + shopId);
        if (StringUtils.isNotEmpty(shopJson) && shopJson!="") {
            return new Gson().fromJson(shopJson, ShopParam.class);
        }*/

        return shopMapper.findOneShopInfo(shopId);
    }

    @Override
    public void updateShop(ShopParam shopParam) {
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopParam,shop);
        //修改门店信息
        shopMapper.updateById(shop);
//        List<Integer> shopStoreIds = new ArrayList<>();
//            QueryWrapper<ShopStore> wrapper = new QueryWrapper<>();
//            wrapper.eq("shop_id",shop.getShopId());
//        List<ShopStore> shopStores = shopStoreMapper.selectList(wrapper);
        QueryWrapper<ShopStore> wrapperDel = new QueryWrapper<>();
        wrapperDel.eq("shop_id",shop.getShopId());
        shopStoreMapper.delete(wrapperDel);
//        int a=1/0;
        for (Store store : shopParam.getStores()) {
            ShopStore shopStore = new ShopStore();
            shopStore.setShopId(shop.getShopId());
            shopStore.setStoreId(store.getStoreId());
            shopStoreMapper.insert(shopStore);
        }
        //存入redis
        shopParam.setShopId(shop.getShopId());
        String shopJson = new Gson().toJson(shopParam);
        System.out.println(shopJson);
        stringRedisTemplate.opsForHash().put("shops","shop:"+shopParam.getShopId(),shopJson);
        stringRedisTemplate.opsForSet().add("shopIds",shopParam.getShopId().toString());
    }

    @Override
    public int deleteShop(Integer shopId) {
        shopMapper.deleteById(shopId);
        QueryWrapper<ShopStore> wrapperDel = new QueryWrapper<>();
        wrapperDel.eq("shop_id",shopId);
        shopStoreMapper.delete(wrapperDel);
        stringRedisTemplate.opsForHash().delete("shops","shop:"+shopId);
        stringRedisTemplate.opsForSet().remove("shopIds",shopId.toString());
        
        return 0;
    }
}
